package com.che.software.testato.domain.dao.jdbc.adao;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

/**
 * Abstract class managing the dataSource recovery and the DbUtils QueryRunner
 * creation.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
public abstract class AbstractDAO {

	/**
	 * Constants.
	 */
	protected static final String AND_CLAUSE = "AND ", COMMA = ", ", SET_CLAUSE = "SET ", WHERE_CLAUSE = "WHERE ";

	/**
	 * Members.
	 */
	private boolean isSetClauseEnabled, isWhereClauseEnabled;
	private DataSource dataSource;

	/**
	 * Create and return the DbUtils QueryRuner.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the QueryRuner newly created.
	 * @since July, 2011.
	 */
	protected QueryRunner getQueryRunner() {
		return new QueryRunner(dataSource);
	}

	/**
	 * Return the beginning of a SQL SET clause according to the
	 * isSetClauseEnabled field.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the COMMA static field if the SQL SET clause has already been
	 *         open, else SET_CLAUSE.
	 * @since July, 2011.
	 */
	protected String getSetClauseBegin() {
		if (isSetClauseEnabled) {
			return COMMA;
		}
		isSetClauseEnabled = true;
		return SET_CLAUSE;
	}

	/**
	 * Return the beginning of a SQL WHERE clause according to the
	 * isWhereClauseEnabled field.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the AND_CLAUSE static field if the SQL WHERE clause has already
	 *         been open, else WHERE_CLAUSE.
	 * @since July, 2011.
	 */
	protected String getWhereClauseBegin() {
		if (isWhereClauseEnabled) {
			return AND_CLAUSE;
		}
		isWhereClauseEnabled = true;
		return WHERE_CLAUSE;
	}

	/**
	 * Getter for the private field value dataSource.
	 * 
	 * @return the dataSource field value.
	 */
	public DataSource getDataSource() {
		return dataSource;
	}

	/**
	 * Setting a value to the dataSource field.
	 * 
	 * @param dataSource the value to set.
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * Getter for the private field value isWhereClauseEnabled.
	 * 
	 * @return the isWhereClauseEnabled field value.
	 */
	public boolean isWhereClauseEnabled() {
		return isWhereClauseEnabled;
	}

	/**
	 * Setting a value to the isWhereClauseEnabled field.
	 * 
	 * @param isWhereClauseEnabled the value to set.
	 */
	public void setWhereClauseEnabled(boolean isWhereClauseEnabled) {
		this.isWhereClauseEnabled = isWhereClauseEnabled;
	}

	/**
	 * Getter for the private field value isSetClauseEnabled.
	 * 
	 * @return the isSetClauseEnabled field value.
	 */
	public boolean isSetClauseEnabled() {
		return isSetClauseEnabled;
	}

	/**
	 * Setting a value to the isSetClauseEnabled field.
	 * 
	 * @param isSetClauseEnabled the value to set.
	 */
	public void setSetClauseEnabled(boolean isSetClauseEnabled) {
		this.isSetClauseEnabled = isSetClauseEnabled;
	}
}